<html>
	<head>
<!-- Basic Hello-world test for the JS2Shapefile and FileSaveTools libraries -->
<!-- Generates a shapefile containing 50 points randomly located between 300000,300000 and 350000,350000 -->
<meta http-equiv="Content-Type" content="text/html;charset=x-user-defined">
<script type="text/javascript" src ="../lib/FileSaveTools.js"></script>
<script type="text/javascript" src="../src/JS2Shapefile.js"></script>
<script type="text/javascript">
	
	function createRandomGraphics(number){
		var graphicArray = [];
		var xmin = 300000, ymin=300000, xmax=350000,ymax=350000;
		for (var i = 0; i<number; i++){
		var trueorfalse = Math.floor(((Math.random() / Math.random()) * 10) % 2) == 0;
		var decimal = 1/(Math.pow(Math.random(),3));
		//var east = Math.random()*50000+xmin;
		//var north =  Math.random()*50000+ymin;
			
		var graphic = 
		{	geometry: 
			{
			x: Math.random()*50000+xmin, 
			y: Math.random()*50000+ymin,
			type: "point"
			},
			attributes:
			{
				ID: i,
				text: "hello",
				dec: decimal,
				log: trueorfalse,
				birthday: "19791203",
				unknownattribute: "boo"
			}
		}
		graphicArray.push(graphic);
		}
		return graphicArray;
	}
	
	function testPointShape(){
        var someGraphics = createRandomGraphics(30);
        var shapewriter = new Shapefile();
		shapewriter.addESRIGraphics(someGraphics);
		var res = shapewriter.getShapefile("POINT");
		// res is object with properties "successful" and "shapefile"
		if (res.successful) {
			var shapefile = res['shapefile'];
			// shapefile is object with properties "shp", "shx" and "dbf" - use these names as file extensions
			var saver = new BinaryHelper();
			for (var actualfile in shapefile) {
				if (shapefile.hasOwnProperty(actualfile)) {
					saver.addData({
						filename: "test_shapefile",
						extension: actualfile,
						datablob: shapefile[actualfile]
					});
				}
			}
			// btn will be created either as normal HTML button that calls saveNative (in Chrome),
			// or flash look-a-like which uses downloadify to save 
			var btn = saver.createSaveControl("saveButtonDiv");
			// in the case of Chrome we could also call saver.saveNative() programatically at this point 
			// without need for user interaction, but downloadify can only save in response to 
			// actual click event on its button	
		}
		else {
			console.error("Error generating shapefile");
		}
	}
	
</script>
</head>
<body>
	<div id = "testbit">
		Click to generate a random shapefile
		<input type="button" value="Create random point shapefile" onclick="testPointShape()">
	</div>
	<div id = "saveButtonDiv">
		Save control will appear here...
	</div>

</body>
</html>